####################################
#    CMakeLists for astroDCAM      #
# (c) Blaise-Florentin Collin 2013 #
#       under GPL v3 License       #
####################################

cmake_minimum_required(VERSION 2.8.6)
# 2.8.6 needed for automoc

####################
# BUILD DIRECTIVES #
####################

# project name
project(astroDCAM)

# checking compiler support for C++11
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
   # get the compiler version
   execute_process(
      COMMAND ${CMAKE_C_COMPILER} -dumpversion
      OUTPUT_VARIABLE GCC_VERSION
      OUTPUT_STRIP_TRAILING_WHITESPACE
   )
   message(STATUS "Gnu gcc version is ${GCC_VERSION}")
   # gcc use two options depending on it's version
   if(GCC_VERSION VERSION_LESS "4.4")
      message(FATAL_ERROR "astroDCAM requires a compiler supporting C++11")
   else(GCC_VERSION VERSION_LESS "4.4")
      message(STATUS "Using the C++11 standard")
      if(GCC_VERSION VERSION_LESS "4.7")
         ADD_DEFINITIONS(-std=c++0x)
      else(GCC_VERSION VERSION_LESS "4.7")
         ADD_DEFINITIONS(-std=c++11)
      endif(GCC_VERSION VERSION_LESS "4.7")
   endif(GCC_VERSION VERSION_LESS "4.4")
else(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
   message(STATUS "Be sure your compiler supports C++11")
endif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")

# version
set(PACKAGE_VERSION 1)
set(MAJOR_VERSION "0")
set(MINOR_VERSION "1")
set(PATCH_VERSION "Beta20141024")
set(BUILD_VERSION "2")
set(FULL_VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D BINARY_VERSION=\\\"${FULL_VERSION}\\\" -D BUILD_VERSION=${BUILD_VERSION}")
message(STATUS "Building astroDCAM ${FULL_VERSION}")

# platform/distribution detection
if(UNIX)
   set(UNIX_FOUND YES)
   if(APPLE)
      set(MACOSX_FOUND YES)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D MACOSX_TARGET")
      message(STATUS "Building for a MacOS X target")
   else(APPLE)
      find_file(DEBIAN_FOUND debian_version debconf.conf PATHS /etc)
      find_file(FEDORA_FOUND fedora-release PATHS /etc)
      find_file(MAGEIA_FOUND mageia-release PATHS /etc)
      message(STATUS "Building for a UNIX target")
   endif(APPLE)
else(UNIX)
   if(WIN32)
      set(WINDOWS_FOUND YES)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D WINDOWS_TARGET -mwindows")
      message(STATUS "Building for a Win32 target")
   else(WIN32)
      message(FATAL_ERROR "Your host is not UNIX nor WIN32 compatible, generation stopped")
   endif(WIN32)
endif(UNIX)

# destir for binary packages
set($ENV{DESTDIR} "${CMAKE_BINARY_DIR}/fakeinstall")

# add cmake module path
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

# needed package Qt4
find_package(Qt4 REQUIRED QtCore QtGui)
if(NOT QT_FOUND)
   message(FATAL_ERROR "Qt4 not found, generation stopped")
endif(NOT QT_FOUND)

# needed package libdc1394
if(WINDOWS_FOUND)
   # header and library in source tree
   set(DC1394_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/windows/libdc1394/include")
   set(DC1394_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/windows/libdc1394/lib/dc1394.dll")
else(WINDOWS_FOUND)
   find_package(Libdc1394 REQUIRED)
   if(NOT DC1394_FOUND)
      message(FATAL_ERROR "Libdc1394 not found, generation stopped")
   endif(NOT DC1394_FOUND)
endif(WINDOWS_FOUND)

# source files
set(astroDCAM_SOURCES
	src/main.cpp
        src/frame.cpp
        src/dc1394bus.cpp
        src/dc1394camera.cpp
	src/QAstroApplication.cpp
	src/QAstroMainWindow.cpp
        src/QAstroMainSettings.cpp
        src/QAstroTab.cpp
        src/QAstroCameraTab.cpp
        src/QAstroRecordTab.cpp
        src/QAstroSettingsTab.cpp
        src/QAstroFrameDisplay.cpp
	)

# we use Qt4
set(CMAKE_AUTOMOC TRUE)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})

# we use Libdc1394
include_directories(${DC1394_INCLUDE_DIRS})

# extra stuff for windows ressources
if(WINDOWS_FOUND)
   message(STATUS "Generating windows rc file...")
   # generate the rc file form template
   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/windows/astroDCAM.rc.in ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.rc @ONLY)
   # generate res file from rc file
   add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.res
      COMMAND windres.exe -I${CMAKE_CURRENT_BINARY_DIR} -i${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.rc -O coff -o ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.res
      MAIN_DEPENDENCY ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.rc)
   # add res file to linker and sources
   set(RES_FILE ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.res)
   set(astroDCAM_SOURCES ${astroDCAM_SOURCES} ${RES_FILE})
else(WINDOWS_FOUND)
   set(RES_FILE "")
endif(WINDOWS_FOUND)


# target
if(MACOSX_FOUND)
   add_executable(astroDCAM MACOSX_BUNDLE ${astroDCAM_SOURCES})
else(MACOSX_FOUND)
   add_executable(astroDCAM ${astroDCAM_SOURCES})
endif(MACOSX_FOUND)

# target libs
target_link_libraries(astroDCAM ${QT_LIBRARIES} ${DC1394_LIBRARIES} ${RES_FILE})

# extra target for doc
find_package(Doxygen)
if(DOXYGEN_FOUND)
   configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/Doxyfile.in ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile @ONLY)
   add_custom_target(doc ALL ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
      COMMENT "Generating API documentation with Doxygen" VERBATIM
      )
endif(DOXYGEN_FOUND)

# installation
if(UNIX_FOUND)
   if(MACOSX_FOUND)
      # bundle properties
      set(MACOSX_BUNDLE_BUNDLE_NAME astroDCAM)
      set(MACOSX_BUNDLE_GUI_IDENTIFIER "astroDCAM")
      set(MACOSX_BUNDLE_ICON_FILE astroDCAM-icon.icns)
      set(MACOSX_BUNDLE_INFO_STRING "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION} Build ${BUILD_VERSION}")
      set(MACOSX_BUNDLE_LONG_VERSION_STRING "${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION} Build ${BUILD_VERSION}")
      set(MACOSX_BUNDLE_SHORT_VERSION_STRING "${MAJOR_VERSION}.${MINOR_VERSION}")
      set(MACOSX_BUNDLE_BUNDLE_VERSION "${PACKAGE_VERSION}")
      set(MACOSX_BUNDLE_COPYRIGHT "(c) 2013 GPLv3 Blaise-Florentin Collin")

      # bundle tree and files
      add_custom_target(
         osx_bundle_tree
         COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.app/Contents/Resources
         COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/macosx/${MACOSX_BUNDLE_ICON_FILE} ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.app/Contents/Resources
         COMMAND mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.app/Contents/Frameworks
         COMMENT "Building bundle tree and copying files"
      )
      add_dependencies(astroDCAM osx_bundle_tree)

      # install libraries in the bundle and fix them
      # BundleUtilities is bugged, item in bundle detection should be patched
      install(CODE "
         include(BundleUtilities)
         fixup_bundle(astroDCAM.app \"\" \"\")
         ")

      # install the bundle to the /Applications folder
      install(
         DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/astroDCAM.app 
         DESTINATION "/Applications"
         USE_SOURCE_PERMISSIONS
         )

   else(MACOSX_FOUND)
      install(TARGETS astroDCAM RUNTIME DESTINATION /usr/bin)
      install(FILES unix/astroDCAM-icon.png DESTINATION /usr/share/icons)
      install(FILES unix/astroDCAM-icon.png DESTINATION /usr/share/astroDCAM/icons)
      install(FILES doc/ChangeLog DESTINATION /usr/share/astroDCAM)
      install(FILES doc/license.txt DESTINATION /usr/share/astroDCAM)
      install(FILES doc/INSTALL DESTINATION /usr/share/astroDCAM)
      install(FILES doc/Contributors DESTINATION /usr/share/astroDCAM)
      install(FILES unix/astroDCAM-entry.desktop DESTINATION /usr/share/applications)
      install(FILES unix/80-firewire.rules DESTINATION /etc/udev/rules.d)
   endif(MACOSX_FOUND)
else(UNIX_FOUND)
   # win 32 target
   install(TARGETS astroDCAM RUNTIME DESTINATION ./)
   install(FILES doc/ChangeLog DESTINATION ./)
   install(FILES doc/license.txt DESTINATION ./)
   install(FILES doc/INSTALL DESTINATION ./)
   install(FILES doc/Contributors DESTINATION ./)
   install(FILES windows/mingwm10.dll DESTINATION ./)
   install(FILES windows/libgcc_s_dw2-1.dll DESTINATION ./)
   install(FILES windows/libstdc++-6.dll DESTINATION ./)
   install(FILES windows/QtCore4.dll DESTINATION ./)
   install(FILES windows/QtGui4.dll DESTINATION ./)
   install(FILES windows/libdc1394/lib/dc1394.dll DESTINATION ./)
   install(FILES windows/pthreadGC2.dll DESTINATION ./)

endif(UNIX_FOUND)


######################
# PACKAGE GENERATION #
######################

# default arch
set(ARCHITECTURE "unkown")

# global directives
set(CPACK_PACKAGE_VERSION_MAJOR ${MAJOR_VERSION})
set(CPACK_PACKAGE_VERSION_MINOR ${MINOR_VERSION})
set(CPACK_PACKAGE_VERSION_PATCH ${PATCH_VERSION})


##################
# source package #
##################

# source package
set(CPACK_SOURCE_GENERATOR TGZ)
set(CPACK_SOURCE_IGNORE_FILES ".svn")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "astroDCAM_${FULL_VERSION}_src" CACHE INTERNAL "tarball basename")


#####################
# binaries packages #
#####################

# global directives for debian, rpm, windows and Mac OS X packages
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Video astronomy toolbox for IIDC/DCAM cameras")
set(CPACK_PACKAGE_DESCRIPTION "astroDCAM is a toolbox for astronomy imaging capture/processing")
set(CPACK_PACKAGE_DESCRIPTION "${CPACK_PACKAGE_DESCRIPTION} using IIDC/DCAM cameras")
set(CPACK_PACKAGE_VENDOR "Thx8411")
set(CPACK_PACKAGE_CONTACT "thx8411@users.sourceforge.net")
set(CPACK_STRIP_FILES astroDCAM)


##################
# debian package #
##################

if(DEBIAN_FOUND)

   # arch detection
   find_program(DPKG_PROGRAM dpkg DOC "dpkg program of Debian-based systems")
   if(DPKG_PROGRAM)
     execute_process(
        COMMAND ${DPKG_PROGRAM} --print-architecture
        OUTPUT_VARIABLE ARCHITECTURE
        OUTPUT_STRIP_TRAILING_WHITESPACE
     )
   endif(DPKG_PROGRAM)

   # settings
   set(CPACK_GENERATOR DEB)
   set(CPACK_SET_DESTDIR "ON")
   set(DEB_EPOCH 0)
   set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE ${ARCHITECTURE})
   set(CPACK_DEBIAN_PACKAGE_NAME "astroDCAM")

   # dependencies
   set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6, libgcc1, libqtcore4
         , libqtgui4, libstdc++6
         , libdc1394-22")
   set(CPACK_DEBIAN_PACKAGE_SECTION graphics)
   set(CPACK_DEBIAN_PACKAGE_VERSION "${DEB_EPOCH}:${FULL_VERSION}-${PACKAGE_VERSION}")
   set(CPACK_PACKAGE_FILE_NAME ${CPACK_DEBIAN_PACKAGE_NAME}_${FULL_VERSION}_${ARCHITECTURE})
   set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "${CPACK_PACKAGE_DESCRIPTION_SUMMARY}\n ${CPACK_PACKAGE_DESCRIPTION}")

endif(DEBIAN_FOUND)


###############
# rpm package #
###############

if(FEDORA_FOUND OR MAGEIA_FOUND)

   # arch detection
   execute_process(COMMAND  uname -m
      OUTPUT_VARIABLE ARCHITECTURE
      OUTPUT_STRIP_TRAILING_WHITESPACE
   )

   # settings
   set(CPACK_GENERATOR RPM)
   set(CPACK_SET_DESTDIR "OFF")
   set(CPACK_RPM_PACKAGE_ARCHITECTURE ${ARCHITECTURE})
   set(CPACK_RPM_PACKAGE_LICENSE "GPL v3")
   set(CPACK_RPM_PACKAGE_GROUP "Productivity/Scientific/Astronomy")
   set(CPACK_RPM_PACKAGE_DESCRIPTION ${CPACK_PACKAGE_DESCRIPTION})
   set(CPACK_RPM_PACKAGE_NAME "astroDCAM")
   set(CPACK_PACKAGE_FILE_NAME ${CPACK_RPM_PACKAGE_NAME}_${FULL_VERSION}.${ARCHITECTURE})
   set(CPACK_RPM_PACKAGE_RELOCATABLE FALSE)

   # dependencies
   if(ARCHITECTURE STREQUAL "x86_64")
      set(CPACK_RPM_PACKAGE_REQUIRES "lib64qtcore4,")
      set(CPACK_RPM_PACKAGE_REQUIRES "${CPACK_RPM_PACKAGE_REQUIRES} lib64qtgui4")
      set(CPACK_RPM_PACKAGE_REQUIRES "${CPACK_RPM_PACKAGE_REQUIRES} lib64dc1394")
   else(ARCHITECTURE STREQUAL "x86_64")
      set(CPACK_RPM_PACKAGE_REQUIRES "libqtcore4,")
      set(CPACK_RPM_PACKAGE_REQUIRES "${CPACK_RPM_PACKAGE_REQUIRES} libqtgui4")
      set(CPACK_RPM_PACKAGE_REQUIRES "${CPACK_RPM_PACKAGE_REQUIRES} libdc1394")
   endif(ARCHITECTURE STREQUAL "x86_64")

endif(FEDORA_FOUND OR MAGEIA_FOUND)


####################
# Mac Os X package #
####################

if(MACOSX_FOUND)

   # source package
   execute_process(
      COMMAND basename ${CMAKE_CURRENT_SOURCE_DIR}
      OUTPUT_VARIABLE SOURCE_PATH
   )
   add_custom_target(
      package_source
      WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../
      COMMAND tar --exclude=.svn -cf ${CMAKE_CURRENT_BINARY_DIR}/${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar ${SOURCE_PATH}
      COMMAND gzip -f -9 ${CMAKE_CURRENT_BINARY_DIR}/${CPACK_SOURCE_PACKAGE_FILE_NAME}.tar
      COMMENT "Building source archive"
   )

   # binary dmg image for the bundle
   add_custom_target(
      package
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/
      COMMAND make install DESTDIR=${CMAKE_CURRENT_BINARY_DIR}/fakeinstall/
      COMMAND hdiutil create -ov -srcfolder ${CMAKE_CURRENT_BINARY_DIR}/fakeinstall/Applications/astroDCAM.app astroDCAM.dmg
      COMMENT "Building dmg package"
   )

endif(MACOSX_FOUND)


###################
# Windows package #
###################

if(WINDOWS_FOUND)

   set(CPACK_GENERATOR NSIS)
   set(CPACK_NSIS_PACKAGE_NAME "astroDCAM ${MAJOR_VERSION}.${MINOR_VERSION}")
   set(CPACK_NSIS_DISPLAY_NAME "astroDCAM ${MAJOR_VERSION}.${MINOR_VERSION}")
   set(CPACK_PACKAGE_ICON ${CMAKE_SOURCE_DIR}/windows\\\\banner.bmp)
   set(CPACK_NSIS_MUI_ICON ${CMAKE_SOURCE_DIR}/windows/install-icon.ico)
   set(CPACK_NSIS_MUI_UNIICON ${CMAKE_SOURCE_DIR}/windows/uninstall-icon.ico)
   set(CPACK_NSIS_INSTALLED_ICON_NAME ${CMAKE_SOURCE_DIR}/windows/astroDCAM-icon.ico)
   set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/doc/license.txt)
   set(CPACK_NSIS_CONTACT "thx8411@users.sourceforge.net")
   set(CPACK_NSIS_EXTRA_INSTALL_COMMANDS "CreateShortCut \\\"$DESKTOP\\\\${astroDCAM}.lnk\\\" \\\"$INSTDIR\\\\astroDCAM.exe\\\" ")
   set(CPACK_NSIS_EXTRA_UNINSTALL_COMMANDS "Delete \\\"$DESKTOP\\\\astroDCAM.lnk\\\" ")
   set(CPACK_PACKAGE_EXECUTABLES "astroDCAM.exe" "astroDCAM")

endif(WINDOWS_FOUND)


# CPack not included on Mac OS X to avoid conflicts
# with my own dmg bundle compression rule
if(NOT MACOSX_FOUND)
   include(CPack)
endif(NOT MACOSX_FOUND)

#########
# TESTS #
#########

include(CTest)
